package com.firstgroup.dms.controller;

import com.firstgroup.dms.common.Result;
import com.firstgroup.dms.service.StatisticsService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/statistics")
public class StatisticsController {

    @Resource
    private StatisticsService statisticsService;

    @GetMapping("/instock")
    public Result getInStockStats(
            @RequestParam(required = false) String goodsName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        // 计算偏移量
        int start = (page - 1) * limit;
        return statisticsService.getInStockStats(goodsName, startDate, endDate, start, limit);
    }

    @GetMapping("/sales")
    public Result getSalesStats(
            @RequestParam(required = false) String goodsName,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        int start = (page - 1) * limit;
        return statisticsService.getSalesStats(goodsName, startDate, endDate, start, limit);
    }

    @GetMapping("/stock")
    public Result getStockQuantity(
            @RequestParam(required = false) String goodsName,
            @RequestParam(name = "page", defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {
        int start = (page - 1) * limit;
        return statisticsService.getStockQuantity(goodsName, start, limit);
    }
}